package com.lovo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.dao.IStudentDAO;
import com.lovo.model.DOS.StudentDO;
import com.lovo.model.DTO.StudentDTO;
import com.lovo.service.IStudentService;
import com.lovo.util.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description:
 *
 * @author LiuChengQi
 * @date Created on 2020/11/25
 */
@Service
public class StudentServiceImpl
        extends ServiceImpl<IStudentDAO,StudentDO>
        implements IStudentService {

    @Resource
    private IStudentDAO studentDAO;
    @Resource
    private RedisUtil redisUtil;


    @Override
    public Page selectAll(Integer page, Integer size) {
        Page pageObj = null;
        // 判断缓冲中是否存在查询的学生信息
        pageObj = (Page) redisUtil.get("selectAll="+page+"="+size);
        if (pageObj == null) {
            pageObj = new Page(page,size);
            Page selectPage = studentDAO.selectPage(pageObj, null);
            pageObj = selectPage;
            redisUtil.set("selectAll="+page+"="+size,pageObj);
        }

        return pageObj;
    }

    @Override
    public void saveStudent(StudentDTO studentDTO) {
        StudentDO studentDO = new StudentDO();
        BeanUtils.copyProperties(studentDTO,studentDO);
        int insert = studentDAO.insert(studentDO);
        redisUtil.delall();
    }
}
